<template>
  <div class="home">
    <!-- 欢迎横幅 -->
    <div class="welcome-banner">
      <div class="banner-content">
        <h1>欢迎使用互联网医院管理平台</h1>
        <p>高效、智能、专业的医院数字化管理解决方案</p>
        <div class="quick-actions">
          <el-button
            type="primary"
            size="large"
            @click="$router.push('/appointment/list')"
          >
            <el-icon><Calendar /></el-icon>
            预约管理
          </el-button>
          <el-button
            type="success"
            size="large"
            @click="$router.push('/doctor/list')"
          >
            <el-icon><UserFilled /></el-icon>
            医生管理
          </el-button>
          <el-button
            type="info"
            size="large"
            @click="$router.push('/patient/list')"
          >
            <el-icon><Avatar /></el-icon>
            患者管理
          </el-button>
        </div>
      </div>
      <div class="banner-image">
        <div class="image-placeholder">
          <el-icon size="120"><Monitor /></el-icon>
          <p>智慧医疗</p>
        </div>
      </div>
    </div>

    <!-- 数据概览 -->
    <div class="overview-section">
      <h2 class="section-title">数据概览</h2>
      <div class="overview-grid">
        <div class="overview-card" v-for="item in overviewData" :key="item.title">
          <div class="card-icon" :style="{ backgroundColor: item.color }">
            <el-icon size="28"><component :is="item.icon" /></el-icon>
          </div>
          <div class="card-content">
            <h3>{{ item.value }}</h3>
            <p>{{ item.title }}</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 功能模块 -->
    <div class="modules-section">
      <h2 class="section-title">功能模块</h2>
      <div class="modules-grid">
        <div
          class="module-card"
          v-for="module in visibleModules"
          :key="module.name"
          @click="navigateToModule(module)"
        >
          <div class="module-icon" :style="{ backgroundColor: module.color }">
            <el-icon size="32"><component :is="module.icon" /></el-icon>
          </div>
          <div class="module-info">
            <h4>{{ module.name }}</h4>
            <p>{{ module.description }}</p>
          </div>
          <div class="module-arrow">
            <el-icon><ArrowRight /></el-icon>
          </div>
        </div>
      </div>
    </div>

    <!-- 系统状态 -->
    <div class="status-section">
      <h2 class="section-title">系统状态</h2>
      <div class="status-grid">
        <div class="status-card">
          <h4>
            <el-icon><TrendCharts /></el-icon>
            系统性能
          </h4>
          <div class="status-content">
            <div class="status-item">
              <span>CPU使用率</span>
              <el-progress :percentage="65" color="#409eff" />
            </div>
            <div class="status-item">
              <span>内存使用率</span>
              <el-progress :percentage="42" color="#67c23a" />
            </div>
            <div class="status-item">
              <span>磁盘使用率</span>
              <el-progress :percentage="78" color="#e6a23c" />
            </div>
          </div>
        </div>
        <div class="status-card">
          <h4>
            <el-icon><Connection /></el-icon>
            服务状态
          </h4>
          <div class="status-content">
            <div class="service-item">
              <span>数据库服务</span>
              <el-tag type="success">正常</el-tag>
            </div>
            <div class="service-item">
              <span>文件服务</span>
              <el-tag type="success">正常</el-tag>
            </div>
            <div class="service-item">
              <span>消息队列</span>
              <el-tag type="success">正常</el-tag>
            </div>
            <div class="service-item">
              <span>缓存服务</span>
              <el-tag type="warning">警告</el-tag>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快捷操作 -->
    <div class="shortcuts-section">
      <h2 class="section-title">快捷操作</h2>
      <div class="shortcuts-grid">
        <div class="shortcut-item" @click="$router.push('/appointment/list')">
          <el-icon><Plus /></el-icon>
          <span>新建预约</span>
        </div>
        <div class="shortcut-item" @click="$router.push('/doctor/list')">
          <el-icon><UserFilled /></el-icon>
          <span>添加医生</span>
        </div>
        <div class="shortcut-item" @click="$router.push('/patient/list')">
          <el-icon><Avatar /></el-icon>
          <span>添加患者</span>
        </div>
        <div class="shortcut-item" @click="$router.push('/doctor/schedule')">
          <el-icon><Calendar /></el-icon>
          <span>排班管理</span>
        </div>
        <div class="shortcut-item" @click="$router.push('/finance/billing')">
          <el-icon><Money /></el-icon>
          <span>财务管理</span>
        </div>
        <div class="shortcut-item" @click="$router.push('/system/settings')">
          <el-icon><Setting /></el-icon>
          <span>系统设置</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from "vue";
import { useRouter } from "vue-router";
import { usePermission } from "../composables/usePermission";
import { ElMessage } from "element-plus";
import axios from "axios";

const router = useRouter();
const { isSuperAdmin } = usePermission();

// 数据概览
const overviewData = ref([
  {
    title: "总医生数",
    value: "加载中...",
    icon: "UserFilled",
    color: "#409eff",
  },
  {
    title: "总患者数",
    value: "1,456",
    icon: "Avatar",
    color: "#67c23a",
  },
  {
    title: "今日预约",
    value: "89",
    icon: "Calendar",
    color: "#e6a23c",
  },
  {
    title: "本月收入",
    value: "￥256K",
    icon: "Money",
    color: "#f56c6c",
  },
]);

// 获取医生数量
const getDoctorCount = async () => {
  try {
    console.log('开始获取医生数量...');
    const response = await axios.get('/api/pcdoctor/count');
    console.log('API响应:', response);
    console.log('响应数据:', response.data);
    
    const doctorCount = response.data.data?.count || 0;
    console.log('医生数量:', doctorCount);
    
    // 更新医生数量
    const doctorItem = overviewData.value.find(item => item.title === "总医生数");
    if (doctorItem) {
      doctorItem.value = doctorCount.toString();
      console.log('医生数量已更新为:', doctorItem.value);
    }
  } catch (error) {
    console.error('获取医生数量失败:', error);
    console.error('错误详情:', error.response?.data || error.message);
    ElMessage.error(`获取医生数量失败: ${error.response?.data?.message || error.message}`);
    
    // 错误时显示默认值
    const doctorItem = overviewData.value.find(item => item.title === "总医生数");
    if (doctorItem) {
      doctorItem.value = "0";
    }
  }
};

// 功能模块
const modules = ref([
  {
    name: "医生管理",
    description: "医生信息管理和排班安排",
    icon: "UserFilled",
    color: "#67c23a",
    path: "/doctor/list",
  },
  {
    name: "患者管理",
    description: "患者档案和病历记录管理",
    icon: "Avatar",
    color: "#e6a23c",
    path: "/patient/list",
  },
  {
    name: "预约管理",
    description: "预约创建、修改和状态管理",
    icon: "Calendar",
    color: "#f56c6c",
    path: "/appointment/list",
  },
  {
    name: "财务管理",
    description: "账单管理和财务统计分析",
    icon: "Money",
    color: "#909399",
    path: "/finance/billing",
  },
  {
    name: "系统管理",
    description: "用户权限和系统配置管理",
    icon: "Setting",
    color: "#606266",
    path: "/system/users",
  },
  {
    name: "数据大屏",
    description: "医院运营数据可视化展示",
    icon: "TrendCharts",
    color: "#409EFF",
    path: "/dashboard",
    requireSuperAdmin: true, // 标记需要超级管理员权限
  },
]);

// 过滤功能模块（根据权限显示）
const visibleModules = computed(() => {
  return modules.value.filter(module => {
    // 如果模块需要超级管理员权限，检查用户是否为超级管理员
    if (module.requireSuperAdmin) {
      return isSuperAdmin.value;
    }
    return true;
  });
});

// 方法
const navigateToModule = (module: any) => {
  // 检查权限
  if (module.requireSuperAdmin && !isSuperAdmin.value) {
    ElMessage.error('此功能仅限超级管理员访问');
    return;
  }
  
  router.push(module.path);
};

// 组件挂载时获取数据
onMounted(() => {
  getDoctorCount();
});
</script>

<style lang="scss" scoped>
.home {
  .welcome-banner {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    padding: 60px 40px;
    margin-bottom: 32px;
    color: white;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 300px;

    .banner-content {
      flex: 1;

      h1 {
        font-size: 32px;
        margin: 0 0 16px 0;
        font-weight: 600;
      }

      p {
        font-size: 18px;
        margin: 0 0 32px 0;
        opacity: 0.9;
      }

      .quick-actions {
        display: flex;
        gap: 16px;
        flex-wrap: wrap;

        .el-button {
          height: 48px;
          padding: 0 24px;
          font-size: 16px;
        }
      }
    }

    .banner-image {
      flex: 0 0 200px;
      display: flex;
      justify-content: center;
      align-items: center;

      .image-placeholder {
        text-align: center;
        opacity: 0.8;

        .el-icon {
          margin-bottom: 16px;
        }

        p {
          font-size: 16px;
          margin: 0;
        }
      }
    }
  }

  .section-title {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 24px;
    color: #333;
  }

  .overview-section {
    margin-bottom: 32px;

    .overview-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
      gap: 24px;
    }

    .overview-card {
      background: white;
      border-radius: 12px;
      padding: 24px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
      border: 1px solid #f0f0f0;
      display: flex;
      align-items: center;
      min-height: 100px;
      cursor: pointer;
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
      }

      .card-icon {
        width: 64px;
        height: 64px;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 20px;
        color: white;
        flex-shrink: 0;
      }

      .card-content {
        flex: 1;

        h3 {
          font-size: 28px;
          font-weight: 700;
          margin: 0 0 6px 0;
          color: #333;
          line-height: 1.2;
        }

        p {
          font-size: 15px;
          color: #666;
          margin: 0;
          font-weight: 500;
        }
      }
    }
  }

  .modules-section {
    margin-bottom: 32px;

    .modules-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
      gap: 24px;
    }

    .module-card {
      background: white;
      border-radius: 12px;
      padding: 24px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
      cursor: pointer;
      transition: all 0.3s ease;
      display: flex;
      align-items: center;
      min-height: 100px;
      border: 1px solid #f0f0f0;

      &:hover {
        transform: translateY(-4px);
        box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
        border-color: #e0e0e0;
      }

      .module-icon {
        width: 60px;
        height: 60px;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 20px;
        color: white;
        flex-shrink: 0;
      }

      .module-info {
        flex: 1;
        min-width: 0;

        h4 {
          font-size: 18px;
          font-weight: 600;
          margin: 0 0 6px 0;
          color: #333;
          line-height: 1.3;
        }

        p {
          font-size: 14px;
          color: #666;
          margin: 0;
          line-height: 1.4;
        }
      }

      .module-arrow {
        color: #ccc;
        transition: all 0.3s ease;
        flex-shrink: 0;
        margin-left: 12px;
      }

      &:hover .module-arrow {
        color: #409eff;
        transform: translateX(4px);
      }
    }
  }

  .status-section {
    margin-bottom: 32px;

    .status-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
      gap: 24px;
    }

    .status-card {
      background: white;
      border-radius: 12px;
      padding: 24px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
      border: 1px solid #f0f0f0;
      display: flex;
      flex-direction: column;
      min-height: 280px;
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
      }

      h4 {
        font-size: 18px;
        font-weight: 600;
        margin: 0 0 20px 0;
        color: #333;
        display: flex;
        align-items: center;
        gap: 10px;
        padding-bottom: 12px;
        border-bottom: 1px solid #f5f5f5;

        .el-icon {
          font-size: 20px;
          color: #409eff;
        }
      }

      .status-content {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-evenly;
      }

      .status-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 18px;

        &:last-child {
          margin-bottom: 0;
        }

        span {
          font-size: 15px;
          color: #666;
          font-weight: 500;
          flex: 0 0 90px;
        }

        .el-progress {
          flex: 1;
          margin-left: 20px;
        }
      }

      .service-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 16px;
        padding: 12px 16px;
        background: #f8f9fa;
        border-radius: 8px;
        transition: background-color 0.2s;

        &:hover {
          background: #f0f2f5;
        }

        &:last-child {
          margin-bottom: 0;
        }

        span {
          font-size: 15px;
          color: #666;
          font-weight: 500;
        }

        .el-tag {
          font-weight: 500;
        }
      }
    }
  }

  .shortcuts-section {
    .shortcuts-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
      gap: 16px;

      .shortcut-item {
        background: white;
        border-radius: 8px;
        padding: 24px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        text-align: center;
        cursor: pointer;
        transition: all 0.2s;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 12px;

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
        }

        .el-icon {
          font-size: 32px;
          color: #409eff;
        }

        span {
          font-size: 14px;
          color: #333;
          font-weight: 500;
        }
      }
    }
  }
}

// 中等屏幕响应式设计
@media (max-width: 1024px) and (min-width: 769px) {
  .home {
    .overview-section {
      .overview-grid {
        grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
        gap: 20px;
      }

      .overview-card {
        padding: 22px;

        .card-icon {
          width: 60px;
          height: 60px;
        }

        .card-content {
          h3 {
            font-size: 26px;
          }

          p {
            font-size: 14px;
          }
        }
      }
    }

    .status-section {
      .status-grid {
        grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
        gap: 20px;
      }

      .status-card {
        padding: 22px;
        min-height: 260px;

        h4 {
          font-size: 17px;
          
          .el-icon {
            font-size: 19px;
          }
        }

        .status-item {
          margin-bottom: 16px;

          span {
            font-size: 14px;
            flex: 0 0 85px;
          }

          .el-progress {
            margin-left: 18px;
          }
        }

        .service-item {
          padding: 11px 14px;
          margin-bottom: 14px;

          span {
            font-size: 14px;
          }
        }
      }
    }

    .modules-section {
      .modules-grid {
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        gap: 20px;
      }

      .module-card {
        padding: 20px;

        .module-icon {
          width: 56px;
          height: 56px;
        }

        .module-info {
          h4 {
            font-size: 17px;
          }

          p {
            font-size: 13px;
          }
        }
      }
    }
  }
}

// 小屏幕响应式设计
@media (max-width: 768px) {
  .home {
    .welcome-banner {
      flex-direction: column;
      text-align: center;
      padding: 40px 24px;

      .banner-content {
        margin-bottom: 32px;

        h1 {
          font-size: 24px;
        }

        p {
          font-size: 16px;
        }

        .quick-actions {
          justify-content: center;

          .el-button {
            height: 40px;
            font-size: 14px;
          }
        }
      }

      .banner-image {
        flex: none;
      }
    }

    .section-title {
      font-size: 18px;
    }

    .overview-section {
      .overview-grid {
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 16px;
      }

      .overview-card {
        padding: 20px;
        min-height: 80px;

        .card-icon {
          width: 52px;
          height: 52px;
          margin-right: 16px;
        }

        .card-content {
          h3 {
            font-size: 24px;
          }

          p {
            font-size: 14px;
          }
        }
      }
    }

    .modules-section {
      .modules-grid {
        grid-template-columns: 1fr;
        gap: 16px;
      }

      .module-card {
        padding: 20px;
        min-height: 80px;

        .module-icon {
          width: 48px;
          height: 48px;
          margin-right: 16px;
        }

        .module-info {
          h4 {
            font-size: 16px;
          }

          p {
            font-size: 13px;
          }
        }
      }
    }

    .status-section {
      .status-grid {
        grid-template-columns: 1fr;
        gap: 16px;
      }

      .status-card {
        padding: 20px;
        min-height: 240px;

        h4 {
          font-size: 16px;
          margin-bottom: 16px;
          
          .el-icon {
            font-size: 18px;
          }
        }

        .status-item {
          margin-bottom: 14px;

          span {
            font-size: 14px;
            flex: 0 0 80px;
          }

          .el-progress {
            margin-left: 16px;
          }
        }

        .service-item {
          padding: 10px 12px;
          margin-bottom: 12px;

          span {
            font-size: 14px;
          }
        }
      }
    }

    .shortcuts-section {
      .shortcuts-grid {
        grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));

        .shortcut-item {
          padding: 16px;

          .el-icon {
            font-size: 24px;
          }

          span {
            font-size: 12px;
          }
        }
      }
    }
  }
}
</style>